<?php

/**
 * @author Rizky Tahara Shita
 * @website http://tahara.web.id
 * @email rizky.tahara@gmail.com
 * @copyright b'IT Cyber Solution
 */

require_once(APPPATH.'controllers/admin/applications.php');
class Entry extends Applications
{
	public function __construct()
	{
		parent::__construct();

		/**
		 * @description check the session variables
		 */
		$this->M_login->session_check();

		/**
		 * @description the URL
		 */
		$this->bz_segment['END'] = 5;
		$this->bz_segment['URL'] = $this->M_functions->segment_create();
		$this->bz_segment['SITE_URL'] = site_url($this->bz_segment['URL']);

		/**
		 * @description access rights
		 */
		$this->access_rights = $this->M_menu->valid_check('db_default', $this->session->userdata('session_user_id'));
		$this->access_rights = array
			(
				'ADD' => in_array('add', $this->access_rights) ? TRUE : FALSE
				, 'EDIT' => FALSE
				, 'DELETE' => FALSE
			);

		/**
		 * @description initialize variables for database
		 */
		$this->tbl_attr = array
			(
				'TABLE' => array
					(
						'NAME' => 'hris_employee_attendance'
						, 'NAME_ALIAS' => 'hris_employee_attendance a'
						, 'PRIMARY' => array
							(
								'SHOW' => FALSE
								, 'DATA' => array('id')
							)
					)
				, 'FORM' => array
					(
						'FIELDS' => 'id, country__id, name, abbreviation, symbol'
						, 'FIELDS_ORDER' => ''
						, 'COLUMN' => 1
					)
				, 'URL' => array
					(
						'FORM' => $this->bz_segment['SITE_URL'].'/form'
						, 'VIEW' => $this->bz_segment['SITE_URL'].'/view'
					)
			);

		/**
		 * @description template lite variables
		 */
		$this->bz_tlite_var['CACHE_PATH'] = $this->bz_tlite_var['SESSION']['USER_ID'].'|'.str_replace('/', '|', $this->bz_segment['URL']);
		$this->bz_tlite_var['CACHE'] = array
			(
				'PUBLIC' => $this->bz_tlite_var['SESSION']['USER_ID']
				, 'PRIVATE' => array
					(
						'GLOBAL' => $this->bz_tlite_var['CACHE_PATH']
						, 'VIEW' => $this->bz_tlite_var['CACHE_PATH'].'|view'
						, 'FORM' => $this->bz_tlite_var['CACHE_PATH'].'|form'
					)
			);
	}


	/**
	 * @function index()
	 * @description main controller
	 */
	public function index()
	{
		if ($this->web_data['BISKITZ']['HEADER']['BROWSER_SUPPORTED'] == '')
		{
			/**
			 * @description if the user has ADD or EDIT access, show the form!
			 */
			if ($this->access_rights['ADD'] || ($this->access_rights['EDIT']))
			{
				/**
				 * @description if there isn't any css/ajax cache, build it! else show cache!
				 */
				if ( ! $this->bz_tlite->is_cached($this->bz_tlite_var['NAMES']['CSS'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']))
				{
					/**
					 * @section begin page init
					 */
						/**
						 * @description show the menu
						 */
						$menu = $this->M_menu->shows();
						$this->web_data['JAVASCRIPT_MENU'] = $menu['JAVASCRIPT_MENU'];
						$this->web_data['PAGE']['MENU'] = $menu['PAGE']['MENU'];

						/**
						 * @description init the css and the ajax needed
						 */
						$css_files = array
							(
								array('layout' => 'screen')
								, array('tipsy/tipsy' => 'screen')
								, array('validate/validate' => 'screen')
								, array('form_icon_bar' => 'screen')
								, array('nanoscroller/nanoscroller' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? 'screen' : NULL)
								, array('shortcut_bar' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? 'screen' : NULL)
								, array('ddm/multi-ddm' => 'screen')
							);
						$js_files = array
							(
								'js/plugins/ddm' => array('jquery.multi-ddm.pack')
								, 'js/plugins/nanoscroller' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? array('jquery.nanoscroller.min', 'overthrow.min') : NULL
								, 'js/plugins/validate' => array('jquery.validate.pack', 'jquery.metadata')
								, 'js/plugins/tipsy' => array('jquery.tipsy.pack')
								, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_JS_FUNCTIONS_PATH'] => array('common', 'form', 'error')
								, 'themes/'.$this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN'].'/'.$this->web_data['BISKITZ']['SETTINGS']['BISKITZ_JS_PATH'] => array('common')
							);


						/**
						 * @section begin form initialize
						 */
							/**
							 * @description initialize the form elements
							 */
							//$form_content = '<p>information goes here</p>'

							$form_content = array
							(

								'<b><center>'.$this->lang->line('hris_date').'</b></center><br />'
								, form_input
									(
										array
										(
											'name' => 'txtinput[0]', 'id' => 'txt0'
											, 'size' => '20', 'maxlength' => '20'
										)
									)

								,'<b><center>'.$this->lang->line('hris_time').'</b></center><br />'
								. form_input
									(
										array
										(
											'name' => 'txtinput[1]', 'id' => 'txt1'
											, 'size' => '20', 'maxlength' => '20'
										)
									)

								, form_submit
									(
										array('id' => 'btn_save')
										, $this->lang->line('hris_in')
									)

								. form_submit
									(
										array('id' => 'btn_update')
										, $this->lang->line('hris_end')
									)
							);

						/**
						 * @description create the form and elements
						 */
						$this->table->clear();
						$form_content = $this->table->make_columns($form_content, $this->tbl_attr['FORM']['COLUMN']);
						$form_content = $this->table->generate($form_content);

						/**
						 * @section end form initialize
						 */
					/**
					 * @section end page init
					 */



					/**
					 * @section begin generate template variables
					 */
						/**
						 * @description css and ajax
						 */
						$this->web_data['PAGE']['CSS_FILES'] = $this->M_template->css_generate($css_files, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN']);
						$this->web_data['PAGE']['JAVASCRIPT_FILES'] = $this->M_template->js_generate($js_files);

						/**
						 * @description initialize the additional javascript
						 */
						$this->web_data['JAVASCRIPT'] .= '
							<script>
								$(function()
								{
									$("#btn_save").click
										(function()
										{
											js_form_action_validate
											(
												"'.$this->bz_segment['SITE_URL'].'", "ajx_", "insert", "0"
												, "#frm_header", "#process", "#error_message"
												, "validate"
											);
										});

									$("#btn_update").click
										(function()
										{
											js_form_action_validate
											(
												"'.$this->bz_segment['SITE_URL'].'", "ajx_", "insert", "1"
												, "#frm_header", "#process", "#error_message"
												, "validate"
											);
										});
						';

						if ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error')
						{
							$this->web_data['JAVASCRIPT'] .= '$(".nano").nanoScroller({preventPageScrolling: true});';
						}

						$this->web_data['JAVASCRIPT'] .= '
									$("#btn_save").focus().select();
								});
							</script>
						';

						/**
						 * @description menu descriptions
						 */
						$this->web_data['PAGE']['DETAIL'] = $this->M_menu->detail();

						/**
						 * @description form dialogs
						 */
						$this->web_data['PAGE']['FORM'] = array
							(
								'OPEN' => form_open('', array('id' => 'frm_header', 'onSubmit' => 'return false'), '')
								, 'CONTENT' => $form_content
								, 'CENTER' => 'TRUE'
							);
					/**
					 * @section end generate template variables
					 */
				}


				/**
				 * @description log the action!
				 */
				$this->M_log->write('db_default', sprintf($this->lang->line('log_form_access'), $this->router->class, 'form'));
			}
			else
			{
				/**
				 * @description doesn't have ADD or EDIT access? redirect to desktop
				 */
				redirect('admin/desktop');
			}



		}
		else
		{
			/**
			 * @section Browser NOT Supported
			 */
				/**
				 * @description browser not supported message error
				 */
				$css_files = array(array('layout' => 'screen'));


				/**
				 * @section begin generate template variables
				 */
					/**
					 * @description css and ajax
					 */
					$this->web_data['PAGE']['CSS_FILES'] = $this->M_template->css_generate($css_files, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN']);
				/**
				 * @section end generate template variables
				 */
		}



		/**
		 * @description output template variables
		 */
		$this->bz_tlite->assign('WEB_DATA', $this->web_data);


		/**
		 * @section begin output
		 */
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['HEADER'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CSS'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['MENU'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_DESCRIPTION'], $this->bz_tlite_var['CACHE']['PRIVATE']['GLOBAL']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_FORM'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_SHORTCUT_BAR'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['FOOTER'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['JAVASCRIPT'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['FOOTER_END'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		/**
		 * @section end output
		 */
	}



/**
 * @section AJAX Action Section
 */

	/**
	 * @function ajx_insert()
	 * @description insert the data
	 */
	public function ajx_insert($prm_key)
	{
		if ($this->input->is_ajax_request() && ($this->access_rights['ADD'] || $this->access_rights['EDIT']))
		{
			//$this->db_default->query('call `sp_hris_employee_attendance_sett`(?, ?, ?)', array($this->session->userdata('session_user_id'), $this->input->post('txtinput[0]'), $this->input->ip_address()));

			$this->db_default->query('call `sp_hris_employee_attendance_inout`(?, ?, ?, ?, ?)', array($this->session->userdata('session_user_id'), $this->input->post('txtinput[0]'), $this->input->post('txtinput[1]'), $prm_key, $this->input->ip_address()));

			/**
			 * @description change the button status
			 */

#			if ($return_value[0] == 'js_action_update')
#			{
#				$return_value = array_merge
#					(
#						$return_value
#						, array($this->access_rights)
#					);
#			}


#			$return_value = json_encode($return_value);
#			echo $return_value;

			$error_message = array($this->lang->line('hris_save'), 'ok', 'desktop');


			$return_value = array
				(
					'js_action_message'
					, array($error_message[0])
				);


			$return_value = json_encode($return_value);
			echo $return_value;



		}
	}

}

/** End of file entry.php */
/** Location: ./biskitz_mpc/biskitz/controllers/admin/hris/attendance/presence/entry.php */
